<template>
  <div class="edit">
    <!-- 我的频道 -->
    <van-cell>
      <div slot="title">
        我的频道
      </div>
      <div>
        <van-button plain size="mini" round type="danger" @click="editBtn">{{edit}}</van-button>
      </div>
    </van-cell>
    <van-grid class="my-grid">
      <van-grid-item v-for="(item,index) in userChannels" :key="index" @click="editMyChannels(item,index)">
        <!-- 自定义我的频道的文本 -->
        <div slot="text" :class="active===index?'text_red':''">{{ item.name }}</div>
        <!-- 自定义图标 -->
        <van-icon v-show="isEdit&&item.id!==0" class="icon_mychannels" slot="icon" name="close"></van-icon>
      </van-grid-item>
    </van-grid>
    <!-- 编辑-------------------------------- -->
    <van-cell>
        <div slot="title">
          点击添加频道
        </div>
    </van-cell>
    <van-grid class="recmomend-grid">
      <van-grid-item v-for="(item,index) in filterChannnels"
      :key="index"
      :text="item.name"
      @click="pushChannnels(item)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  editUserChannels,
  removeUserChannels
} from '@/api/channels/channels'
import { mapState } from 'vuex'
export default {
  name: 'channelsEdit',
  data () {
    return {
      // 存储全部频道的数组
      allChannels: [],
      // 控制编辑按钮的
      isEdit: false,
      edit: '编辑'
    }
  },
  props: {
    // 父组件传过来的用户频道
    userChannels: {
      type: Array,
      required: true
    },
    // 让频道高亮
    active: {
      type: Number,
      required: true
    }
  },

  created () {
    this.getallChannels()
  },

  methods: {
    // 获取全部频道的函数
    async getallChannels () {
      try {
        const { data: res } = await getAllChannels()
        // console.log(res.data)
        this.allChannels = res.data.channels
      } catch (err) {
        this.$toast.fail('获取全部频道失败！')
      }
    },
    // 点击添加频道就push频道--------------------------------
    async pushChannnels (item) {
      this.userChannels.push(item)
      // 如果用户登录了,就发起请求，永久存储
      if (this.userTokens) {
        try {
          await editUserChannels({
            id: item.id,
            seq: this.userChannels.length
          })
          // console.log(res)
        } catch (err) {
          this.userChannels.pop()
          this.$toast.fail('编辑频道失败，请重试！')
        }
      } else {
        // 用户未登录，就存到本地
        localStorage.setItem('local_channels', JSON.stringify(this.userChannels))
      }
    },
    // 点击编辑就显示×图标，且改变按钮里的显示文本
    editBtn () {
      this.isEdit = !this.isEdit
      this.edit = this.isEdit ? '完成' : '编辑'
    },
    // 点击频道，进行编辑
    async editMyChannels (item, index) {
      if (index === 0) return false
      this.userChannels.splice(index, 1)
      if (index < this.active) {
        this.$emit('updateActive', this.active - 1, true)
      }

      // 这里是删除阶段
      if (this.isEdit) {
        // 开始永久性的删除操作
        if (this.userTokens) {
          // 用户登录了
          try {
            const res = await removeUserChannels(item)
            console.log(res)
          } catch (err) {
            this.userChannels.push(item)
            this.$toast.fail('删除频道失败！')
          }
        } else {
          // 用户未登录
          localStorage.setItem('local_channels', JSON.stringify(this.userChannels))
        }
      } else {
        // 这是编辑阶段
        // console.log(item, index)
        this.$emit('updateActive', index)
      }
    }
  },
  computed: {
    ...mapState(['userTokens']),
    filterChannnels () {
      const newArr = []
      this.allChannels.forEach(channels => {
        // 返回id一样的元素
        const res = this.userChannels.find(userChannelsItem => {
          return userChannelsItem.id === channels.id
        })
        if (res) {
          return false
        } else {
          return newArr.push(channels)
        }
      })
      return newArr
    }
  }
}
</script>

<style scoped lang='less'>
.edit {
  padding-top: 100px;
  height: 100%;
}
.van-button__text {
  letter-spacing: 0.5em;
}
.van-grid {
  padding-left: 22px;
  ::v-deep.van-grid-item__content {
  display: flex;
  padding: 0;
  height: 80px;
  width: 160px;
  background-color:rgb(243, 238, 238);
  border-radius: 40px;
  margin-bottom: 20px;
  &::after {
    border: unset;
  }
}
}
.my-grid {
  ::v-deep .icon_mychannels {
    font-size: 32px;
    position: absolute;
    top: -30px;
    left: 50px;
    opacity: .5;
  }
  .van-grid-item {
    .van-grid-item__content {
      div {
        font-size: 28px;
      }
      .text_red {
        color: red;
      }
    }
  }
}
</style>
